define([ 'config', 'text!app/module/system/org.html', 'laypage',
		'artDialog', 'autocomplete' ], function(config, viewHTML, laypage,
		artDialog, autocomplete) {

	function Item(data) {
		this.id = data.id;
		this.orgName = data.departName;
		this.orgCode = data.departID;
		this.parentOrg = data.pname;
	}

	var model = avalon.define({
		$id : "orgs",
		orgName : "",
		orgCode : "",
		parentName : "",
		orgInfo : {},
		orgList : [],
		pagesize : 15,
		onInit : function() {
			avalon.log('ok');
		},
		paginate : function(page) {
			var curr = page || 1;
			var parameters = "?pageIndex=" + curr + "&limit=" + model.pagesize +"&orgName=" + model.orgName + "&orgCode=" + model.orgCode + "&parentName=" + model.parentName;
			var url = "admin/org/orgList" + parameters;
			$.ajax({
				type : "GET",
				url : url,
				success : function(data, status) {
					var orgs = $.map(data.rows, function(item) {
						return new Item(item)
					});
					model.orgList = [];
					model.orgList.pushArray(orgs);
					// 调用分页
					laypage({
						cont : 'pager',
						pages : Math.ceil(data.results / model.pagesize),
						curr : curr || 1,
						jump : function(obj, first) {
							if (!first) {
								model.paginate(obj.curr);
							}
						}
					});
				},
				error : function(responseData, textStatus, errorThrown) {
					dialog().title('异常提示').content('系统出现异常').button([ {
						value : '确定',
						autofocus : true,
						callback : function() {
						}
					} ]).showModal();
				}
			});

		},
		edit : function(orgInfo) {
			if (orgInfo === -1) { // 新建组织机构
				model.orgInfo = {};
				url = "admin/orgCreate";
			} else {              // 编辑组织机构
				model.orgInfo = orgInfo;
				url = "admin/orgUpdate";
			}
			debugger;
			// 自动补全
			$("#parentOrg").autocomplete({
				serviceUrl: "admin/org/searchOrgName",
                showNoSuggestionNotice: true,
                noSuggestionNotice: 'Sorry,no matching results',
                // 数据处理
                transformResult: function(response) {
                    var obj = $.parseJSON(response);
                    return {
                        suggestions: $.map(obj, function(data) {
                            return { value: data.departName, data: data.departId };
                        })
                    };
                },
                // 点击或回车选择
                onSelect: function(suggestion) {
                    model.orgInfo.departName = suggestion.value;
                }
			});
			// 弹出层
			dialog({
				title : '编辑用户信息',
				content : $('#editOrg'),
				okValue : "确定",
				ok : function() {
					// 提交修改
					$.post(url, $("#orgForm").serialize());
				},
				cancelValue : "取消",
				cancel : function() {
				},
			}).showModal();
		},
		del : function(el) {
			dialog({
				title : "确认操作",
				content : "确定删除？ 此操作不可恢复！",
				okValue : "确定",
				ok : function() {
					$.post("admin/org/delete", {"id" : el}, function(data) {
						if (data) {
							model.paginate();
						}
					})
				},
				cancelValue : "取消",
				cancel : function() {}
			}).showModal();
		}
	});
		
	require([ 'jquery', 'domReady!' ], function($) {
	});

	// 定义视图模块
	var viewmod = {
		viewtSrc : viewHTML,
		viewInit : function() {
			model.paginate();
		}, // 模块view加载完成后的回调方法，在刷新（F5）时可能调用两次
		viewDestroy : function() {
		} // 切换菜单时销毁原来的view
	};
	return viewmod;
});
